# -*- coding: utf-8 -*-
# time: 2025/8/9 16:16
# file: models.py
# author: kai
# email: 1071664616@qq.com

"""
用户 models
管理员和普通用户
"""
from django.db import models
from django.contrib.auth.models import AbstractUser


class User(AbstractUser):
    """用户表"""
    USER_TYPE_CHOICES = (
        ("admin", '管理员'),
        ("user", '普通用户'),
    )
    user_type = models.CharField(
        max_length=20,
        choices=USER_TYPE_CHOICES,
        default="user",
        verbose_name="用户类型"
    )
    # 额外的用户信息字段
    phone_number = models.CharField(max_length=200, unique=True, verbose_name='手机', help_text='用户登录的主要凭证')
    avatar = models.ImageField(upload_to='avatars/', default='avatars/default.png', blank=True, null=True, verbose_name='头像')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    # 设置手机号为 USERNAME_FIELD
    USERNAME_FIELD = 'phone_number'
    REQUIRED_FIELDS = ['username']

    def is_admin(self):
        return self.user_type == 'admin'

    def __str__(self):
        return f"{self.username} ({'管理员' if self.is_admin() else '普通用户'})"

    class Meta:
        db_table = 'users'
        verbose_name = '用户'
        verbose_name_plural = '用户'
